package cn.chencaiju.day12;

import org.junit.Test;

public class RegexTest {
	/**
	 * 非负整数
	 * @param num
	 * @return
	 */
	public boolean isPositive(String positive) {
		//首位不为0的数字，或者0
//		String regex="[1-9][0-9]*|0";
//		String regex="[1-9]\\d*|0";
		String regex="[0-9]|[1-9][0-9]+";//0,或者首位不为0的数字
		return positive.matches(regex);
	}
	@Test public void testPositive() {
		System.out.println(isPositive("0"));
		System.out.println(isPositive("123k"));
		System.out.println(isPositive("123"));
		System.out.println(isPositive("0123"));
		System.out.println(isPositive("-123"));
	}
	/**
	 * 整数
	 * @param num
	 * @return
	 */
	public boolean isInteger(String integer) {
		String regex="[-+]?\\d*|0";
		return integer.matches(regex);
	}
	@Test public void testInteger() {
		System.out.println(isInteger("-123"));
		System.out.println(isInteger("+0"));
		System.out.println(isInteger("123"));
		System.out.println(isInteger("123o"));
	}
	/**
	 * 图片
	 * @param file
	 * @return
	 */
	public boolean isImage(String file) {
		String regex=".+\\.(jpg|jpeg|gif|png)";
		return file.matches(regex);
	}
	@Test public void testImage() {
		System.out.println(isImage("12.3.jpeg"));
		System.out.println(isImage("123.jpg"));
		System.out.println(isImage(".jpg"));
		System.out.println(isImage("123jpg"));
		System.out.println(isImage("jpg.123"));
		
	}
	public boolean isExcel(String fileName) {
		return fileName.matches(".*\\.(xls|xlsx)");
		
	}
	@Test public void testRegex() {
		String str="sds.xlsx";
		System.out.println(isExcel(str));
	}
	@Test public void testReplace() {
		String str="asd34d,fad34343edfdsdlg,f";
		String result=str.replaceAll("[0-9]", "n");
		System.out.println(result);
	}
	//首字符
	@Test public void testCart() {
		String regex="^abc.*";
		System.out.println("abc".matches(regex));
		System.out.println("adc".matches(regex));
		System.out.println("abcd".matches(regex));
	}
	@Test public void testEnd() {
		String regex=".*abc$";
		System.out.println("abc,".matches(regex));
	}
	//
	@Test public void test() {
//		String regex="hhh*m";
//		System.out.println("hm".matches(regex));
//		System.out.println("hhm".matches(regex));
//		System.out.println("hhhhm".matches(regex));
		String regex="(abc)*end";
		System.out.println("abcend".matches(regex));
		System.out.println("abccend".matches(regex));
		System.out.println("abcabcend".matches(regex));
	}
	
	//断言
	@Test public void testPassword() {
		//6到18为字符
//		String regex="\\w{6,18}";
		//断言 必须含有数字，且6到18为字符
//		String regex="(?=.*\\d+.*)\\w{6,18}";
		//断言 必须含有数字，且6到18为字符,首字母大写
//		String regex="(?=.*\\d+.*)(?=^[A-Z])\\w{6,18}"; 
		String regex="(?=.*\\d+.*)(?=^[A-Z])\\w{5,17}";
		System.out.println("M123456u".matches(regex));
		System.out.println("jj9hhhu".matches(regex));
		System.out.println("M2s3d4rf34sd124dfv4d".matches(regex));
		System.out.println("sdd1122asd%".matches(regex));
		
	}
	
	@Test public void testURL() {
		String regex="^(?:([A-Za-z]+):)?(\\/{0,3})([0-9.\\-A-Za-z]+)(?::(\\d+))?(?:\\/([^?#]*))?(?:\\?([^#]*))?(?:#(.*))?$";
		System.out.println("https://www.cnblogs.com/xnn1993/p/7606189.html".matches(regex));
		System.out.println("https://blog.csdn.net/qq_24653023/article/details/52234799".matches(regex));
	}
}
